package com.example.my.ui.reflex;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.example.my.R;
import com.example.my.bean.PersonBean;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Created by wsl
 * @des 反射
 * @date 2022/4/27
 */
public class ReflexActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, ReflexActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reflex);

        TextView tvContent = findViewById(R.id.tvContent);
        tvContent.animate()
                .setDuration(2000)
                .translationYBy(200)
                .translationXBy(300)
                .start();

        try {
            Class<PersonBean> aClass = PersonBean.class;
//            Class<?> aClass = Class.forName("com.example.my.bean.PersonBean");
            Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
            declaredConstructor.setAccessible(true);
            Object o = declaredConstructor.newInstance();
            Method setAge = aClass.getMethod("setAge", int.class);
            setAge.setAccessible(true);
            setAge.invoke(o, 10);

            Field ageField = aClass.getDeclaredField("age");
            ageField.setAccessible(true);
            ageField.setInt(o, 10000);

            Method getAge = aClass.getMethod("getAge");
            getAge.setAccessible(true);
            int age = (int) getAge.invoke(o);
            Log.e("age", age + "");


            Class<PersonBean> personBeanClass1 = PersonBean.class;
            Constructor<PersonBean> declaredConstructor1 = personBeanClass1.getDeclaredConstructor();
            declaredConstructor1.setAccessible(true);

            Field age1 = personBeanClass1.getDeclaredField("age");
            age1.setAccessible(true);


            PersonBean o1 = (PersonBean) declaredConstructor1.newInstance();
            Log.e("ReflexActivity-o1-1", o1.getAge() + "");

            o1.setAge(1000);
            Log.e("ReflexActivity-o1-2", o1.getAge() + "");

            age1.set(o1, 200);
            Log.e("ReflexActivity-o1-3", age1.get(o1) + "");

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("age", e.getMessage());
        }
    }
}
